package com.freedy.concurrent.Monitors.reentrantLock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import static com.freedy.concurrent.Monitors.Sleeper.sleep;

/**
 * 锁超时
 * @author Freedy
 * @date 2021/4/22 20:45
 */
@Slf4j
public class Test3 {
    private static final ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            log.info("尝试获取锁");
            try {
                if (lock.tryLock(2, TimeUnit.SECONDS)){
                    try {
                        log.info("获取到锁");
                    } finally {
                        lock.unlock();
                    }
                }else {
                    log.info("获取不到锁");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        lock.lock();
        t1.start();
        sleep(1);
        //t1.interrupt();
    }
}
